package cease.command.cease;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;

import cease.butter.Constants;
import cease.command.HasOptionCommand;
import cease.update.Updater;
import cease.update.Version;
import cease.urs.Session;

/**
 * @author dhf
 */
public class DownloadAppCommand extends HasOptionCommand {

    @Option(name = "-d", metaVar = "required", required = true, usage = "download directory path")
    private String directory;

    @Option(name = "-v", metaVar = "optional", usage = "specified app version to download")
    private String version;

    public String getCmdDesc() {
        return "download " + Constants.APP_NAME;
    }

    private void reset() {
        directory = null;
        version = null;
    }

    public void run(Session session, String[] args) throws Exception {
        try {
            parser.parseArgument(args);
            Version v = null;
            if (null == version) {
                v = Version.getLastestVersion();
            } else {
                v = Version.parse(version);
            }
            Updater.download(v, directory);
        } catch (CmdLineException e) {
            parser.printUsage(System.out);
        } finally {
            reset();
        }
    }
}
